package com.shaomengjie.smjmvp.view.core;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.shaomengjie.smjmvp.R;
import com.shaomengjie.smjmvp.framework.AppStateTracker;
import com.shaomengjie.smjmvp.framework.Trace;
import com.shaomengjie.smjmvp.utils.Constants;
import com.shaomengjie.smjmvp.view.activity.MainActivity;
import com.shaomengjie.smjmvp.widget.Dialog;
import com.shaomengjie.smjmvp.widget.SToolbar;

/**
 * Created by shaomengjie on 2017/4/7.
 * pdsshaomengjie@live.com
 */

public abstract class BaseActivity extends AppCompatActivity {
    private SToolbar mToolbar;
    private Dialog dialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (AppStateTracker.getInstance().getAppState() != AppStateTracker.APP_STATE_INIT_OR_KILLED) {
            setContentView();
            initToolbar();
            initView();
            initData(savedInstanceState);
        } else {
            protectApp();
        }
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        this.setContentView(layoutResID, false);
    }
    public void setContentView(@LayoutRes int layoutResID, boolean hasTitle) {
        if (hasTitle) {
            ViewGroup root = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.base_layout_title, null);
            LayoutInflater.from(this).inflate(layoutResID, root);
            super.setContentView(root);
        } else {
            super.setContentView(layoutResID);
        }
    }

    /**
     * 设置视图
     */
    protected abstract void setContentView();

    /**
     * 初始化toolbar
     */
    protected void initToolbar() {
        mToolbar = (SToolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            translucentStatus();
            setSupportActionBar(mToolbar);
            if (hasLeftIcon())
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            if (titleCenter()) {
                mToolbar.setTextGravity(SToolbar.GRAVITY_CENTER);
            } else {
                mToolbar.setTextGravity(SToolbar.GRAVITY_LEFT);
            }
            mToolbar.setTitle(getSupportActionBar().getTitle().toString());
        }
    }

    /**
     * 设置状态栏透明
     */
    protected void translucentStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 是否让左边的返回icon显示
     * @return
     */
    protected boolean hasLeftIcon() {
        return true;
    }

    /**
     * 是否让title居中显示
     * @return
     */
    protected boolean titleCenter() {
        return true;
    }

    /**
     * 初始化view
     */
    protected abstract void initView();
    /**
     * 处理数据
     *
     * @param savedInstanceState 数据状态恢复
     */
    protected abstract void initData(Bundle savedInstanceState);
    /**
     * 应用被强杀重启app
     */
    protected void protectApp() {
        Trace.d("protectApp " + getClass().getSimpleName() + " killed will do reStart");
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(Constants.KEY_ACTION, Constants.ACTION_RESTART_APP);
        startActivity(intent);
        overridePendingTransition(0, 0);
        finish();
    }
    @Override
    protected void onStart() {
        if (AppStateTracker.getInstance().isShowGesture()) {
            Trace.d("show gesture activity");
            AppStateTracker.getInstance().setScreenOff(false);
        }
        super.onStart();
    }
    public void showProgress(){
        if (dialog == null){
            dialog = new Dialog(this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(false);
            dialog.show();
        }
    }
    public void closeProgress(){
        if (dialog!=null&&dialog.isShowing()){
            dialog.dismiss();
            dialog = null;
        }
    }
}
